//1 声明变量用来缓存组件对象
var taskDatagrid, taskClaimDialog, taskClaimDialogForm, showDetailsDialog, showDetailsDialogForm, taskAdoptDialog,
  taskAdoptDialogForm, querySystemForm, querySystemDialog, showDetailDialog, showDetailForm;
$(function () {
  //2 使用声明的变量来缓存组件
  taskDatagrid = $("#taskDatagrid");
  taskClaimDialog = $("#taskClaimDialog");
  taskClaimDialogForm = $("#taskClaimDialogForm");
  showDetailsDialog = $("#showDetailsDialog");
  showDetailsDialogForm = $("#showDetailsDialogForm");
  taskAdoptDialog = $("#taskAdoptDialog");
  taskAdoptDialogForm = $("#taskAdoptDialogForm");
  querySystemForm = $("#querySystemForm");
  querySystemDialog = $("#querySystemDialog");
  showDetailDialog = $("#showDetailDialog");
  showDetailForm = $("#showDetailForm")

  //3 创建一个命令对象，存放所有的按钮的点击事件的回调
  cmdObj = {
    taskClaim: function () {
      //row有两个表单
      row = taskDatagrid.datagrid("getSelected");
      if (!row) {
        $.messager.alert("提示", "请选中一个任务认领", "info");
        return;
      }
      //console.debug(row);
      taskClaimDialogForm.form("clear");//清空表单
      taskClaimDialogForm.form("load", row);
      taskClaimDialog.dialog("open").dialog("center");//打开模态框
    },
    taskCancel: function () {
      taskClaimDialog.dialog("close");
    },
    taskAdopt: function () {
      taskClaimDialogForm.form('submit', {
        url: "task/adopt",
        onsubmit: function () {
          return taskClaimDialogForm.form("validate");
        },
        success: function (data) {
          var result = $.parseJSON(data);
          if (result.success) {
            taskClaimDialog.dialog("close");
            taskDatagrid.datagrid("reload");
          } else {
            $.messager.alert("提示", result.message, "info");
          }
        }
      })
    },
    queryScode: function () {
      querySystemForm.form("clear");
      querySystemDialog.dialog("open").dialog("center");
    },
    taskUnadopt: function () {
      taskClaimDialogForm.form('submit', {
        url: "task/unadopt",
        onsubmit: function () {
          return taskClaimDialogForm.form("validate");
        },
        success: function (data) {
          //console.debug(data);
          var result = $.parseJSON(data);
          //console.debug(result);
          if (result.success) {
            taskClaimDialog.dialog("close");
            taskDatagrid.datagrid("reload");
          } else {
            $.messager.alert("提示", result.message, "info");
          }
        }
      })
    },
    taskCancel: function () {
      taskClaimDialog.dialog("close");
    },
    queryCommit: function () {
      querySystemForm.form('submit', {
        url: "autho/queryApply",
        onsubmit: function () {
          return querySystemForm.form("validate");
        },
        success: function (data) {
          //console.debug(data);
          var result = $.parseJSON(data);
          //console.debug(result)
          //console.debug(aa);
          if (result.success) {
            querySystemDialog.dialog("close");
            if (result.task==null) {
              $.messager.alert("提示", result.scode , "info");
            }
            else {
              console.debug(result.scode);
              console.debug(result.task);
              showDetailForm.form("clear");
              showDetailForm.form("load", result.task);
              showDetailDialog.dialog("open").dialog("center");
              //$.messager.alert("提示", result.scode+"xxxx", "info");
            }
          } else {
            $.messager.alert("提示", result.message, "info");
          }
        }
      })
    },
    queryCancel: function () {
      //$.messager.alert("","1111","info");
      querySystemDialog.dialog("close");
    },
    taskAdoptConfirm: function () {
      taskAdoptDialogForm.form('submit', {
        url: "task/confirm",
        onsubmit: function () {
          return taskAdoptDialogForm.form("validate");
        },
        success: function (data) {
          console.debug(data);
          var result = $.parseJSON(data);
          if (result.success) {
            taskAdoptDialogForm.form("clear");
            taskAdoptDialog.dialog("close");
            taskDatagrid.datagrid("reload");
          } else {
            $.messager.alert("提示", result.message, "info");
          }
        }
      })
    },
    ok: function () {
      showDetailDialog.dialog("close");
      // $('#aa').val()
      // if() {
      //   $("#aa").hide();
      // } else {
      //   $("#aa").show();
      // }
    },
    search: function () {
      console.debug($("#searchForm").serializeJSON());
      taskDatagrid.datagrid("load", $("#searchForm").serializeJSON());
    }
  };

  //4 初始化组件
  //4.1 初始化数据表格
  taskDatagrid.datagrid({
    title: '申请列表',
    url: 'task/page',
    pagination: true,
    rownumbers: true,
    singleSelect: true,
    fit: true,
    nowrap: false,
    pageList: [10, 15, 20],
    pageSize: 10,
    fitColumns: true,//列宽度
    columns: [[
      {field: 'state', title: '状态', width: 100, formatter: taskStateFormatter},
      // {field: 'name', title: '申请系统名称', width: 100},
      // {field: 'version', title: '申请系统版本', width: 100},
      {field: 'applicant', title: '申请人', width: 100},
      // {field: 'applyrea', title: '申请原因', width: 100},
      {field: 'applycode', title: '申请码', width: 100},
      // {field: 'applydate', title: '申请日期', width: 100},
      // {field: 'deadline', title: '申请截至日期', width: 100},
      {field: 'assignee', title: '办理人', width: 100},
      // {field: 'denyrea', title: '拒绝原因', width: 100},
    ]],
    toolbar: '#taskDatagridToolbar',
    onDblClickRow: function (rowIndex, rowData) {
      console.debug(rowData);
      var a = rowData;
      showDetailForm.form("clear");
      showDetailForm.form("load", rowData);
      // var item = $('div[name]');
      // var status = {
      //   1:'未认领',
      //   2:'已完成（完成）',
      //   3:"已完成（拒绝）",
      // }
      // item.each(function () {
      //   var $this = $(this);
      //   $this.text(a[$this.attr('name')]);
      // });
      // $("#sda").text(status[a.state]);
      showDetailDialog.dialog("open").dialog("center");
    }
  });

  //4.2初始化申请表单
  taskClaimDialog.dialog({
    title: '申请单审核',
    width: 300,
    height: 400,
    closed: true,
    modal: true,
    buttons: '#taskClaimDialogToolbar'
  });

  //4.3 初始化通过申请模态框
  taskAdoptDialog.dialog({
    title: '通过申请',
    width: 300,
    height: 300,
    closed: true,
    modal: true,
    buttons: '#taskAdoptDialogToolbar'
  });
  //4.4 初始化申请码查询dialog
  querySystemDialog.dialog({
    title: "申请码查询",
    iconCls: "icon-search",
    width: 400,
    height: 150,
    closed: true,
    modal: true,
    buttons: "#querySystemDialogToolbar"
  });

  //4.5 初始化展示数据dialog
  showDetailDialog.dialog({
    title: '详细数据',
    width: 800,
    height: 400,
    closed: true,
    modal: true,
    buttons: '#showDetailDialogToolbar'
  });


  //5 为页面上的所有的a标签注册点击事件
  $("a").on("click", function () {
    //获取标签的data-cmd属性值
    var cmd = $(this).data("cmd");
    if (cmd) {
      cmdObj[cmd + ""]();
    }
  });
});
